#include "stdio.h"
#include "string.h"

void subString1(char *str, char *result, int s, int e) {
    char *temp = str;
    int count = 0;
    while (*temp) {
        if (count >= s && count < e) {
            *result = *temp;
            result++;
        }
        count++;
        temp++;

    }
    result = "\0";


}

/**
 * 使用二级指针
 */
void subString2(char *str, char **result, int s, int e) {
    char *temp = str;//定义临时指针，不影响str指针
    int count = 0;
    char resultArr[e - s + 1];//合理分配，截取多少用多少
    for (int i = s; i < e; ++i) {
        resultArr[count] = *(temp + i);
        count++;
    }
    //不能让一级指针指向容器，因为容器会被回收
//    *result = resultArr;
    resultArr[count] = '\0';
    strcpy(result, resultArr);


}

void subString3(char *str, char *result, int s, int e) {

}



int main() {

    char *str = "nihaowoyehao";
    char result[5 - 2 + 1];
    //字符串截取
//    subString1(str, result, 2, 5);
//    printf("截取之后的字符串是%s\n", result);
    subString2(str, &result, 2, 5);
    printf("---截取之后的字符串是%s\n", result);

    char *result2;
    strncpy(result2, str+2, 5-2);
    printf("====截取之后的字符串是%s\n", result2);
    return 0;
}